package cn.tedu.oa.common.service.impl;

import cn.tedu.oa.common.dao.BaseDao;
import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    private BaseDao<T> baseDao;

    @Override
    public void add(T t) {
        baseDao.add(t);
    }

    @Override
    public void update(T t) {
        baseDao.update(t);
    }

    @Override
    public void delete(Serializable id) {
        baseDao.delete(id);
    }

    @Override
    public void deleteByIds(Integer... ids) {
        if (ids == null) {
            throw new ServiceException("删除失败：参数错误");
        }
        /*多项删除时不选任何数据异常*/
        if (ids.length < 1) {
            throw new ServiceException("删除失败：没有选择需要删除的数据");
        }
        for (Integer id : ids) {
            if (id < 1) {
                throw new ServiceException("删除失败：参数错误");
            }
        }
        baseDao.deleteByIds(ids);
    }

    @Override
    public T get(Serializable id) {
        return baseDao.get(id);
    }

    @Override
    public List<T> getAll() {
        return baseDao.getAll();
    }

    @Override
    public int getRowCount(Object criteria) {
        return baseDao.getRowCount(criteria);
    }

    @Override
    public int getRowCount(Object criteria, String startTime, String endTime) {
        return baseDao.getRowCount(criteria, startTime,endTime);
    }

    @Override
    public int getRowCount() {
        return baseDao.getRowCount();
    }
}
